package org.bayareadiplomacy.tournamatic

object Shuffler {
  val rand = new scala.util.Random();

  def swap[T](xs: Array[T], i: Int, j: Int) = {
    val t = xs(i)
    xs(i) = xs(j)
    xs(j) = t
  }
  
  def fisherYatesShuffle[T](xs: Array[T]) = {
    for (i <- xs.indices.reverse)
      swap(xs, i, rand.nextInt(i + 1))
  }
  
  def functionalShuffle[T](xs: Array[T]) = {
    def shuf(xs: List[T]): List[T] = xs match {
    case List() => List()
    case xs => { 
        val i = rand.nextInt(xs.size);
        xs(i) :: shuf(xs.take(i) ++ xs.drop(i+1))
      }
    }
    shuf(xs.toList).copyToArray(xs, 0)
  }
  
  
}
